package com.vcs.frm.web.client;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.vcs.frm.domain.Client;
import com.vcs.frm.domain.Job;
import com.vcs.frm.domain.User;
import com.vcs.frm.service.client.ClientService;
import com.vcs.frm.utils.Constants;
import com.vcs.frm.web.form.client.NewClientRegistrationFormBean;
import com.vcs.frm.web.form.client.NewJobFormBean;

/**
 * This controller handles requests for Clients - post new job - list jobs
 * posted by client - delete job
 * 
 * @author V319214
 * 
 */

@Controller
@RequestMapping(value = "/c")
public class ClientController {

	/* Logger object */
	private final Logger log = Logger.getLogger(this.getClass());

	@Autowired
	ClientService clientService;

	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	public ClientController() {
	}

	/**
	 * prepares post new job form
	 * 
	 * @return
	 */
	@Secured(Constants.ROLE_CLIENT)
	@RequestMapping(value = "/newJob", method = RequestMethod.GET)
	public ModelAndView initNewJobForm() {

		ModelAndView mav = new ModelAndView();

		/* form bean */
		NewJobFormBean newJobForm = new NewJobFormBean();
		mav.addObject("newJobForm", newJobForm);

		mav.setViewName("newJob");

		return mav;
	}

	/**
	 * Securing this method so that only clients should be able to post a new
	 * job.
	 * 
	 * @param newJobForm
	 * @param bindingResult
	 * @param request
	 * @return {@link ModelAndView}
	 */
	@Secured(Constants.ROLE_CLIENT)
	@RequestMapping(value = "/newJob", method = RequestMethod.POST)
	public ModelAndView processNewJobForm(
			@ModelAttribute("newJobForm") @Valid NewJobFormBean newJobForm,
			BindingResult bindingResult, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();

		log.info("@@@@@@@@@@ inside jobPost(post) method");

		/* form bean */
		if (bindingResult.hasErrors()) {

			mav.addObject("newJobForm", newJobForm);
			mav.setViewName("newJob");
			return mav;
		}

		clientService.postNewJob(newJobForm, request);
		mav.setViewName("redirect:/home");
		return mav;
	}

	/**
	 * Initializes new Client Registration Form
	 * 
	 * @return {@link ModelAndView}
	 */

	@RequestMapping(value = "/reg", method = RequestMethod.GET)
	public ModelAndView initNewClientRegForm() {

		ModelAndView mav = new ModelAndView();

		/* form bean */
		NewClientRegistrationFormBean clientRegForm = new NewClientRegistrationFormBean();
		mav.addObject("clientRegForm", clientRegForm);

		mav.setViewName("clientReg");

		return mav;
	}

	@RequestMapping(value = "/reg", method = RequestMethod.POST)
	public ModelAndView processNewClientRegForm(
			@ModelAttribute("clientRegForm") @Valid NewClientRegistrationFormBean clientRegForm,
			BindingResult bindingResult, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();

		System.out
				.println("@@@@@@@@@@ inside New Client Registration (post) method");
		System.out.println(clientRegForm);
		System.out.println(request.getSession());

		/* form bean */
		mav.addObject("newJobForm", clientRegForm);

		if (bindingResult.hasErrors()) {

			mav.setViewName("clientReg");
			return mav;
		}

		Client client = new Client();
		client.setClientName(clientRegForm.getCompanyName());
		client.setClientProfile(clientRegForm.getProfile());

		User user = new User();
		user.setEnabled(1);
		user.setUsername(clientRegForm.getUserName());
		user.setPassword(clientRegForm.getPassword());

		clientService.registerClient(client, user);

		mav.setViewName("redirect:/home");

		return mav;
	}

	/**
	 * Gets Logged in client's profile based on id from session.
	 * 
	 * @param clientId
	 * @return
	 */
	@Secured(Constants.ROLE_CLIENT)
	@RequestMapping(value = "/profile")
	public ModelAndView getClientProfileByIdFromSession(
			HttpServletRequest request) {

		log.info("method - getClientProfileByIdFromSession");
		ModelAndView mav = new ModelAndView();

		HttpSession session = request.getSession();

		Client c = (Client) request.getSession().getAttribute("client");

		if (null == c) {
			mav.setViewName("redirect:/home");
			return mav;
		}

		mav.addObject("clientProfile", c);

		// set view name - look in client-tiles.xml for definition
		mav.setViewName("showClientProfile");
		return mav;
	}

	@Secured(Constants.ROLE_CLIENT)
	@RequestMapping(value = "/jobs")
	public ModelAndView getJobsPostedByClient(HttpServletRequest request) {
		log.info("method - getJobsPostedByClient");
		ModelAndView mav = new ModelAndView();

		HttpSession session = request.getSession();
		Client client = (Client) session.getAttribute("client");

		List<Job> jobs = null;
		if (client != null) {
			jobs = clientService.getJobsPostedByClient(client.getId());
		}
		mav.addObject("jobs", jobs);
		// set view name - look in client-tiles.xml for definition
		mav.setViewName("listJobsPostedByClient");
		return mav;
	}

}
